package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlSevlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");

		// 获取客户端传过来的xml字符串，并解析为xml对象
		String xml_str = request.getParameter("xmlStr");
		try {
			Document document = DocumentHelper.parseText(xml_str);

			Element rootElement = document.getRootElement();

			Element element1 = rootElement.element("driverClassName");
			Element element2 = rootElement.element("url");
			Element element3 = rootElement.element("username");
			Element element4 = rootElement.element("password");

			System.out.println(element1.getText());
			System.out.println(element2.getText());
			System.out.println(element3.getText());
			System.out.println(element4.getText());

		

		} catch (DocumentException e) {
			e.printStackTrace();
		}
		
		
		
		/**
		 * 要返回一个XML的字符串。客户端的jQuery接收到之后，会自动将XML的字符串转为XML的文档对象。
		 * 因为JQuery。post(,,,"xml") 会把返回的字符串对象转为xml对象
		 */

		PrintWriter out = response.getWriter();
		StringBuffer str_xml = new StringBuffer();
		str_xml.append("<root>");
		for (int i = 0; i < 5; i++) {
			str_xml.append("<userinfo userid=\""+i+"\">");
			str_xml.append("<username>用户"+i+"</username>");
			str_xml.append("<age>12</age>");
			str_xml.append("</userinfo>");
		}
		str_xml.append("</root>");
		out.print(str_xml.toString());
		System.out.println(str_xml.toString());
		out.flush();
		out.close();
		
		

	}

}
